package com.aishuang.order.web;


import com.aishuang.feign.bean.User;
import com.aishuang.feign.client.UserClient;
import com.aishuang.order.pojo.Order;

import com.aishuang.order.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("order")
public class OrderController {

   @Autowired
   private OrderService orderService;
    @Autowired
   private RestTemplate restTemplate;
    @Autowired
    private UserClient userClient;

    @GetMapping("{orderId}")
    public Order queryOrderByUserId(@PathVariable("orderId") Long orderId, HttpServletRequest request) {
        String config = request.getHeader("config");
        System.out.println(config);
        // 1 通过订单id 获取订单数据
        Order order = orderService.queryOrderById(orderId);
        // 2 获取用户id
        Long userId = order.getUserId();
        // 3 调用用户请求 获取用户信息
        // String url = "http://localhost:8081/user/"+userId;
//        String url = "http://userservice:8081/user/"+userId;
//
//        User user = restTemplate.getForObject(url, User.class);
        User user = userClient.queryById(userId);

        // 4 将user 存储到 order 中
        order.setUser(user);



        return order;
    }
}
